import { createStore } from "vuex";

export default createStore({
	state: {
		userinfo: JSON.parse(localStorage.getItem("userinfo")) || null,
		dialog: JSON.parse(localStorage.getItem("dialog")) || {
			isShow: false,
			title: "提示",
			content:
				"由于部分原因，页面上的跳转按钮偶尔会出现失灵，关闭页面重新打开即可。",
		},
		// 文章保存状态
		saveStatus: JSON.parse(sessionStorage.getItem("saveStatus")) || true,
	},
	getters: {
		token: (state) => {
			return state.userinfo ? state.userinfo.token : "";
		},
	},
	mutations: {
		setUserinfo(state, userinfo) {
			state.userinfo = userinfo;
			localStorage.setItem("userinfo", JSON.stringify(userinfo));
		},
		clearUserInfo(state) {
			state.userinfo = null;
			localStorage.removeItem("userinfo");
		},
		setDialogInfo(state, dialog) {
			state.dialog = dialog;
			localStorage.setItem("dialog", JSON.stringify(dialog));
		},
		saveStatus(state, status) {
			state.saveStatus = status;
			sessionStorage.setItem("saveStatus", status);
		},
	},

	actions: {},
	modules: {},
});
